<!DOCTYPE html>
<html lang="zh-CN,en,default">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|Amita:300,300italic,400,400italic,700,700italic|Montserrat:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"dpbirder.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":true,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

<script>
    (function(d, w, c) {
        w.ChatraID = 'Z8KH5JTPhxGC84tKb';
        var s = d.createElement('script');
        w[c] = w[c] || function() {
            (w[c].q = w[c].q || []).push(arguments);
        };
        s.async = true;
        s.src = 'https://call.chatra.io/chatra.js';
        if (d.head) d.head.appendChild(s);
    })(document, window, 'Chatra');
</script>
  <meta name="description" content="数组相关算法  二分查找  算法框架 查找大于等于target的元素下标 12345678910int lowerBound(int []nums, int target)&amp;#123;    int l&#x3D;0,r&#x3D;nums.length,mid;    while(l&lt;r)&amp;#123;        mid &#x3D; l+(r-l)&#x2F;2;        if(nums[mid]&gt;&#x3D;targe">
<meta property="og:type" content="article">
<meta property="og:title" content="基础算法">
<meta property="og:url" content="http://dpbirder.gitee.io/2021/04/03/%E5%9F%BA%E7%A1%80%E7%AE%97%E6%B3%95/index.html">
<meta property="og:site_name" content="dpBirder&#39;s Blog">
<meta property="og:description" content="数组相关算法  二分查找  算法框架 查找大于等于target的元素下标 12345678910int lowerBound(int []nums, int target)&amp;#123;    int l&#x3D;0,r&#x3D;nums.length,mid;    while(l&lt;r)&amp;#123;        mid &#x3D; l+(r-l)&#x2F;2;        if(nums[mid]&gt;&#x3D;targe">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230507204251211.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230426222843697.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230427210935017.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230507211220047.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506170529702.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506163627287.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230505223140112.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506215809865.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506215940117.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230507200008224.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506215839409.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506220018533.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230507195948359.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506220136263.png">
<meta property="og:image" content="https://code-thinking-1253855093.file.myqcloud.com/pics/20220925103433.png">
<meta property="article:published_time" content="2021-04-03T04:23:39.000Z">
<meta property="article:modified_time" content="2023-05-08T06:43:53.205Z">
<meta property="article:author" content="dpbirder">
<meta property="article:tag" content="算法">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230507204251211.png">

<link rel="canonical" href="http://dpbirder.gitee.io/2021/04/03/%E5%9F%BA%E7%A1%80%E7%AE%97%E6%B3%95/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<script> 
 (function(){
        if(''){
            if (prompt('请输入文章密码') !== ''){
                alert('密码错误！');
                history.back();
            }
        }
    })();
</script>
  <title>基础算法 | dpBirder's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">dpBirder's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">15</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">26</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">51</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/zhengzp2019" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://dpbirder.gitee.io/2021/04/03/%E5%9F%BA%E7%A1%80%E7%AE%97%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/boy.jpg">
      <meta itemprop="name" content="dpbirder">
      <meta itemprop="description" content="目前研一就读于西安电子科技大学">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="dpBirder's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          基础算法
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-04-03 12:23:39" itemprop="dateCreated datePublished" datetime="2021-04-03T12:23:39+08:00">2021-04-03</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-05-08 14:43:53" itemprop="dateModified" datetime="2023-05-08T14:43:53+08:00">2023-05-08</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">算法</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>15k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>13 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="数组相关算法"><a class="markdownIt-Anchor" href="#数组相关算法"></a> 数组相关算法</h1>
<h2 id="二分查找"><a class="markdownIt-Anchor" href="#二分查找"></a> 二分查找</h2>
<h3 id="算法框架"><a class="markdownIt-Anchor" href="#算法框架"></a> 算法框架</h3>
<p><strong>查找大于等于target的元素下标</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">lowerBound</span><span class="params">(<span class="keyword">int</span> []nums, <span class="keyword">int</span> target)</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> l=<span class="number">0</span>,r=nums.length,mid;</span><br><span class="line">    <span class="keyword">while</span>(l&lt;r)&#123;</span><br><span class="line">        mid = l+(r-l)/<span class="number">2</span>;</span><br><span class="line">        <span class="keyword">if</span>(nums[mid]&gt;=target) r=mid;</span><br><span class="line">    <span class="keyword">else</span> l=mid+<span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(l==nums.length || nums[l]!=target) <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> l;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<p><strong>查找大于target的元素下标</strong></p>
<blockquote>
<p>等价于使用lowBound查找大于等于target+1下标</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lowerBound(nums,target+<span class="number">1</span>);</span><br></pre></td></tr></table></figure>
<p><strong>查找小于target的元素下标</strong></p>
<blockquote>
<p>等价于使用lowBound查找大于等于target下标减1</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lowerBound(target)-<span class="number">1</span>;</span><br></pre></td></tr></table></figure>
<p><strong>查找小于等于target的元素下标</strong></p>
<blockquote>
<p>等价于使用lowBound查找大于target元素下标，减1</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lowerBound(target+<span class="number">1</span>)-<span class="number">1</span>;</span><br></pre></td></tr></table></figure>
<h3 id="题目汇总"><a class="markdownIt-Anchor" href="#题目汇总"></a> 题目汇总</h3>
<table>
<thead>
<tr>
<th>题目</th>
<th>题解</th>
</tr>
</thead>
<tbody>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/binary-search/">704.二分查找</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/binary-search/solution/shi-yong-lowerboundcha-zhao-by-dpbirder-46f7/">https://leetcode.cn/problems/binary-search/solution/shi-yong-lowerboundcha-zhao-by-dpbirder-46f7/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/description/">34.在排序数组中查找元素的第一个和最后一个位置</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/solutions/2260756/yi-ge-han-shu-lowerboundchu-li-bu-tong-w-d3y3/">https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/solutions/2260756/yi-ge-han-shu-lowerboundchu-li-bu-tong-w-d3y3/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/search-in-rotated-sorted-array/description/">33.搜索旋转数组</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/search-in-rotated-sorted-array/solution/jing-dian-er-fen-fen-lei-tao-lun-shi-jia-esso/">https://leetcode.cn/problems/search-in-rotated-sorted-array/solution/jing-dian-er-fen-fen-lei-tao-lun-shi-jia-esso/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/find-peak-element/description/">162.寻找峰值</a></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/sum-of-distances/">2615.等值距离和</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/sum-of-distances/solution/ha-xi-biao-qian-zhui-he-er-fen-cha-zhao-d4md3/">https://leetcode.cn/problems/sum-of-distances/solution/ha-xi-biao-qian-zhui-he-er-fen-cha-zhao-d4md3/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/minimum-operations-to-make-all-array-elements-equal/">2602.使数组元素全部相等的最少操作次数</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/minimum-operations-to-make-all-array-elements-equal/solution/yi-zhi-yi-mu-biao-yuan-su-cha-xun-qi-ta-dfxka/">https://leetcode.cn/problems/minimum-operations-to-make-all-array-elements-equal/solution/yi-zhi-yi-mu-biao-yuan-su-cha-xun-qi-ta-dfxka/</a></td>
</tr>
</tbody>
</table>
<h3 id="704二分查找"><a class="markdownIt-Anchor" href="#704二分查找"></a> 704.二分查找</h3>
<h4 id="题目描述"><a class="markdownIt-Anchor" href="#题目描述"></a> 题目描述</h4>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230507204251211.png" alt="image-20230507204251211"></p>
<h4 id="题解"><a class="markdownIt-Anchor" href="#题解"></a> 题解</h4>
<h5 id="我的思路"><a class="markdownIt-Anchor" href="#我的思路"></a> 我的思路</h5>
<p>这里使用<code>lowbound</code>函数，查找target下确界，当left等于数组长度，或者nums[left]不等于target时元素不存在。</p>
<h5 id="我的代码"><a class="markdownIt-Anchor" href="#我的代码"></a> 我的代码</h5>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">search</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> l=<span class="number">0</span>,r=nums.length;</span><br><span class="line">        <span class="keyword">while</span>(l&lt;r)&#123;</span><br><span class="line">            <span class="keyword">int</span> mid=l+(r-l)/<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">if</span>(nums[mid]&lt;target) l=mid+<span class="number">1</span>;</span><br><span class="line">            <span class="keyword">else</span> r=mid;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(l==nums.length || nums[l]!=target) <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">return</span> l;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="34在排序数组中查找元素的第一个和最后一个位置"><a class="markdownIt-Anchor" href="#34在排序数组中查找元素的第一个和最后一个位置"></a> 34.在排序数组中查找元素的第一个和最后一个位置</h3>
<h4 id="题目描述-2"><a class="markdownIt-Anchor" href="#题目描述-2"></a> 题目描述</h4>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230426222843697.png" alt="image-20230426222843697"></p>
<h4 id="题解-2"><a class="markdownIt-Anchor" href="#题解-2"></a> 题解</h4>
<h5 id="我的思路-2"><a class="markdownIt-Anchor" href="#我的思路-2"></a> 我的思路</h5>
<p>定义一个<code>lowerBound</code>函数，查找元素下确界，当left为数组长度或者nums[left]不为target时元素不存在。target开始位置相相当于调用<code>lowerBound(nums,target)</code>，而target结束位置相当于调用<code>lowerBound(nums,target+1)-1</code>;</p>
<h5 id="我的代码-2"><a class="markdownIt-Anchor" href="#我的代码-2"></a> 我的代码</h5>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 查找target的下确界</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">int</span> <span class="title">lowerBound</span><span class="params">(<span class="keyword">int</span> []nums,<span class="keyword">int</span> target)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> l=<span class="number">0</span>,r=nums.length;</span><br><span class="line">        <span class="keyword">while</span>(l&lt;r)&#123;</span><br><span class="line">            <span class="keyword">int</span> mid=l+(r-l)/<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">if</span>(nums[mid]&lt;target) l=mid+<span class="number">1</span>;</span><br><span class="line">            <span class="keyword">else</span> r=mid;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> l;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] searchRange(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target) &#123;</span><br><span class="line">        <span class="keyword">int</span> begin=lowerBound(nums,target);</span><br><span class="line">        <span class="keyword">if</span>(begin==nums.length || nums[begin]!=target) <span class="keyword">return</span> <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;-<span class="number">1</span>,-<span class="number">1</span>&#125;;</span><br><span class="line">        <span class="keyword">int</span> end=lowerBound(nums,target+<span class="number">1</span>)-<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;begin,end&#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="双指针"><a class="markdownIt-Anchor" href="#双指针"></a> 双指针</h2>
<h3 id="题目汇总-2"><a class="markdownIt-Anchor" href="#题目汇总-2"></a> 题目汇总</h3>
<table>
<thead>
<tr>
<th>题目</th>
<th>题解</th>
</tr>
</thead>
<tbody>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/remove-duplicates-from-sorted-array/">26. 删除有序数组中的重复项</a></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/remove-element/">27. 移除元素</a></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/move-zeroes/">283. 移动零</a></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/reverse-string/">344. 反转字符串</a></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/">167. 两数之和 II - 输入有序数组</a></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/longest-palindromic-substring/">5. 最长回文子串</a></td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="滑动窗口"><a class="markdownIt-Anchor" href="#滑动窗口"></a> 滑动窗口</h2>
<p>算法的大致逻辑如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> left = <span class="number">0</span>, right = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> (left &lt; right &amp;&amp; right &lt; s.size()) &#123;</span><br><span class="line">    <span class="comment">// 增大窗口</span></span><br><span class="line">    window.add(s[right]);</span><br><span class="line">    right++;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">while</span> (window needs shrink) &#123;</span><br><span class="line">        <span class="comment">// 缩小窗口</span></span><br><span class="line">        window.remove(s[left]);</span><br><span class="line">        left++;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>主要考虑如下三个问题：</p>
<ul>
<li>什么时候应该收缩窗口？</li>
<li>什么时候应该扩大窗口？</li>
<li>什么时候应该更新答案？</li>
</ul>
<h3 id="算法框架-2"><a class="markdownIt-Anchor" href="#算法框架-2"></a> 算法框架</h3>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 滑动窗口算法框架 */</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">slidingWindow</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 用合适的数据结构记录窗口中的数据</span></span><br><span class="line">    <span class="built_in">unordered_map</span>&lt;<span class="keyword">char</span>, <span class="keyword">int</span>&gt; window;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">int</span> left = <span class="number">0</span>, right = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span> (right &lt; s.size()) &#123;</span><br><span class="line">        <span class="comment">// c 是将移入窗口的字符</span></span><br><span class="line">        <span class="keyword">char</span> c = s[right];</span><br><span class="line">        winodw.add(c)</span><br><span class="line">        <span class="comment">// 增大窗口</span></span><br><span class="line">        right++;</span><br><span class="line">        <span class="comment">// 进行窗口内数据的一系列更新</span></span><br><span class="line">        ...</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*** debug 输出的位置 ***/</span></span><br><span class="line">        <span class="comment">// 注意在最终的解法代码中不要 print</span></span><br><span class="line">        <span class="comment">// 因为 IO 操作很耗时，可能导致超时</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;window: [%d, %d)\n&quot;</span>, left, right);</span><br><span class="line">        <span class="comment">/********************/</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 判断左侧窗口是否要收缩</span></span><br><span class="line">        <span class="keyword">while</span> (left &lt; right &amp;&amp; window needs shrink) &#123;</span><br><span class="line">            <span class="comment">// d 是将移出窗口的字符</span></span><br><span class="line">            <span class="keyword">char</span> d = s[left];</span><br><span class="line">            winodw.remove(d)</span><br><span class="line">            <span class="comment">// 缩小窗口</span></span><br><span class="line">            left++;</span><br><span class="line">            <span class="comment">// 进行窗口内数据的一系列更新</span></span><br><span class="line">            ...</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="题目汇总-3"><a class="markdownIt-Anchor" href="#题目汇总-3"></a> 题目汇总</h3>
<table>
<thead>
<tr>
<th>题目</th>
<th>题解</th>
</tr>
</thead>
<tbody>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/minimum-size-subarray-sum/">209.长度最小的子数组</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/minimum-size-subarray-sum/solution/hua-dong-chuang-kou-gui-fan-hua-dai-ma-s-qkmm/">https://leetcode.cn/problems/minimum-size-subarray-sum/solution/hua-dong-chuang-kou-gui-fan-hua-dai-ma-s-qkmm/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/longest-substring-without-repeating-characters/">3.无重复字符的最长子串</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/longest-substring-without-repeating-characters/solution/hua-dong-chuang-kou-shi-jian-5ms-by-dpbi-nq6p/">https://leetcode.cn/problems/longest-substring-without-repeating-characters/solution/hua-dong-chuang-kou-shi-jian-5ms-by-dpbi-nq6p/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/fruit-into-baskets/">904.水果成篮</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/fruit-into-baskets/solutions/2260793/hua-dong-chuang-kou-shi-jian-fu-za-du-on-i9bm/">https://leetcode.cn/problems/fruit-into-baskets/solutions/2260793/hua-dong-chuang-kou-shi-jian-fu-za-du-on-i9bm/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/minimum-window-substring/">76.最小覆盖子串</a></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/find-all-anagrams-in-a-string/">438. 找到字符串中所有字母异位词</a></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/permutation-in-string/">567. 字符串的排列</a></td>
<td></td>
</tr>
</tbody>
</table>
<h3 id="209长度最小子数组"><a class="markdownIt-Anchor" href="#209长度最小子数组"></a> 209.长度最小子数组</h3>
<h4 id="题目描述-3"><a class="markdownIt-Anchor" href="#题目描述-3"></a> 题目描述</h4>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230427210935017.png" alt="image-20230427210935017"></p>
<h4 id="题解-3"><a class="markdownIt-Anchor" href="#题解-3"></a> 题解</h4>
<p>这题的思路分析可参考<a target="_blank" rel="noopener" href="https://programmercarl.com/0209.%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.html">代码随想录</a>（<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1tZ4y1q7XE">视频</a>）</p>
<h5 id="我的思路-3"><a class="markdownIt-Anchor" href="#我的思路-3"></a> 我的思路</h5>
<ul>
<li>枚举窗口终点j</li>
<li>将第j个元素加入窗口(扩大窗口)</li>
<li>判断窗口是否需要缩小
<ul>
<li>需要缩小</li>
<li>进行窗口内相关数据操作，ans = min(ans , j-i+1)</li>
<li>移除左端点元素，remove(nums[i])</li>
<li>缩小窗口，i++</li>
</ul>
</li>
</ul>
<h5 id="我的代码-3"><a class="markdownIt-Anchor" href="#我的代码-3"></a> 我的代码</h5>
<p>for循环版本</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">minSubArrayLen</span><span class="params">(<span class="keyword">int</span> target, <span class="keyword">int</span>[] nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = Integer.MAX_VALUE, sum = <span class="number">0</span>;</span><br><span class="line">        <span class="comment">// 枚举窗口终点</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>, j = <span class="number">0</span>; j &lt; nums.length; j++) &#123;</span><br><span class="line">            sum += nums[j];<span class="comment">// 更新窗口相关数据</span></span><br><span class="line">            <span class="comment">// 需要缩小窗口</span></span><br><span class="line">            <span class="keyword">while</span> (sum &gt;= target) &#123;</span><br><span class="line">                ans = Math.min(ans, j - i + <span class="number">1</span>);</span><br><span class="line">                sum -= nums[i];<span class="comment">// 更新窗口相关数据</span></span><br><span class="line">                i++;<span class="comment">// 缩小窗口</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans == Integer.MAX_VALUE ? <span class="number">0</span> : ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>while循环版本</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">minSubArrayLen</span><span class="params">(<span class="keyword">int</span> target, <span class="keyword">int</span>[] nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> left = <span class="number">0</span>, right = <span class="number">0</span>, sum = <span class="number">0</span>, ans = Integer.MAX_VALUE;</span><br><span class="line">        <span class="keyword">while</span> (right &lt; nums.length) &#123;</span><br><span class="line">            <span class="comment">// 窗口扩大</span></span><br><span class="line">            sum += nums[right++];</span><br><span class="line">            <span class="comment">// 判断窗口是否需要缩小</span></span><br><span class="line">            <span class="keyword">while</span> (sum &gt;= target) &#123;</span><br><span class="line">                <span class="comment">// 更新窗口内相关数据</span></span><br><span class="line">                ans = Math.min(ans, right - left + <span class="number">1</span>);</span><br><span class="line">                <span class="comment">// 缩小窗口</span></span><br><span class="line">                sum -= nums[left--];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans == Integer.MAX_VALUE ? <span class="number">0</span> : ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="904水果成篮"><a class="markdownIt-Anchor" href="#904水果成篮"></a> 904.水果成篮</h3>
<h4 id="题目描述-4"><a class="markdownIt-Anchor" href="#题目描述-4"></a> 题目描述</h4>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230507211220047.png" alt="image-20230507211220047"></p>
<h4 id="题解-4"><a class="markdownIt-Anchor" href="#题解-4"></a> 题解</h4>
<h5 id="我的思路-4"><a class="markdownIt-Anchor" href="#我的思路-4"></a> 我的思路</h5>
<p>这题可以抽象成给定一个窗口，计算能够使窗口中只有两种类型数字的最大窗口长度。使用滑动窗口的方法，并用哈希表存储窗口内每种元素出现的次数。</p>
<p><strong>算法过程</strong></p>
<ul>
<li>
<p>枚举窗口终点元素j，把元素j加入窗口</p>
</li>
<li>
<p>若此时窗口元素数目大于2，则不满足题意需要收缩窗口</p>
<ul>
<li>取出窗口左边元素，并令其次数减一</li>
<li>若出现元素次数为0，则从哈希表中删除改元素</li>
</ul>
</li>
<li>
<p>满足题意，将当前窗口长度与ans进行比较，返回大的结果</p>
</li>
<li>
<p>直至所有窗口终点枚举结束，返回ans</p>
</li>
</ul>
<h5 id="我的代码-4"><a class="markdownIt-Anchor" href="#我的代码-4"></a> 我的代码</h5>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">totalFruit</span><span class="params">(<span class="keyword">int</span>[] fruits)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> N = <span class="number">2</span>, ans = <span class="number">0</span>;</span><br><span class="line">        HashMap&lt;Integer,Integer&gt; map = <span class="keyword">new</span> HashMap();</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>,j=<span class="number">0</span>; j&lt;fruits.length; j++)&#123;</span><br><span class="line">            <span class="comment">// 扩大窗口</span></span><br><span class="line">            map.put(fruits[j],map.getOrDefault(fruits[j],<span class="number">0</span>)+<span class="number">1</span>);</span><br><span class="line">            <span class="comment">// 收缩窗口</span></span><br><span class="line">            <span class="keyword">while</span>(map.size() &gt; N)&#123;</span><br><span class="line">                map.put(fruits[i], map.get(fruits[i])-<span class="number">1</span>);</span><br><span class="line">                <span class="keyword">if</span>(map.get(fruits[i])==<span class="number">0</span>) map.remove(fruits[i]);</span><br><span class="line">                i++;</span><br><span class="line">            &#125;</span><br><span class="line">            ans = Math.max(ans,j-i+<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="前缀和"><a class="markdownIt-Anchor" href="#前缀和"></a> 前缀和</h2>
<h3 id="算法框架-3"><a class="markdownIt-Anchor" href="#算法框架-3"></a> 算法框架</h3>
<p>一维前缀和</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 第一种写法,下标从0开始，则preSum[j]表示为nums[0]~nums[j]项和</span></span><br><span class="line"><span class="keyword">int</span> []preSum = <span class="keyword">new</span> <span class="keyword">int</span>[n];</span><br><span class="line">preSum[<span class="number">0</span>]=nums[<span class="number">0</span>];</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;n;i++)&#123;</span><br><span class="line">    preSum[i]=preSum[i-<span class="number">1</span>]+nums[i];</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 第二种写法,下标从1开始，则preSum[j]表示nums[0]~nums[j-1]项和</span></span><br><span class="line"><span class="keyword">int</span> []preSum = <span class="keyword">new</span> <span class="keyword">int</span>[n+<span class="number">1</span>];</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">    preSum[i]=preSum[i-<span class="number">1</span>]+nums[i-<span class="number">1</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>二维前缀和</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> n=matrix.length,m=matrix[<span class="number">0</span>].length;</span><br><span class="line"><span class="comment">// 初始化二维前缀和</span></span><br><span class="line">preSum = <span class="keyword">new</span> <span class="keyword">int</span>[n+<span class="number">1</span>][m+<span class="number">1</span>];</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=m;j++)</span><br><span class="line">        preSum[i][j]=preSum[i-<span class="number">1</span>][j]+preSum[i][j-<span class="number">1</span>]-preSum[i-<span class="number">1</span>][j-<span class="number">1</span>]+matrix[i-<span class="number">1</span>][j-<span class="number">1</span>];</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 使用二维前缀和，计算matrix(x1,y1,x2,y2)区域的元素和</span></span><br><span class="line">preSum[x2+<span class="number">1</span>][y2+<span class="number">1</span>]-preSum[x1][y2+<span class="number">1</span>]-preSum[x2+<span class="number">1</span>][y1]+preSum[x1][y1];</span><br></pre></td></tr></table></figure>
<h3 id="题目汇总-4"><a class="markdownIt-Anchor" href="#题目汇总-4"></a> 题目汇总</h3>
<table>
<thead>
<tr>
<th>习题</th>
<th>题解</th>
</tr>
</thead>
<tbody>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/contiguous-array/">525.连续数组</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/contiguous-array/solution/zi-ding-xiang-xia-fen-xi-shi-jian-fu-za-j3mza/">https://leetcode.cn/problems/contiguous-array/solution/zi-ding-xiang-xia-fen-xi-shi-jian-fu-za-j3mza/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/continuous-subarray-sum/">523.连续的子数组和</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/continuous-subarray-sum/solution/zi-ding-xiang-xia-fen-xi-shi-jian-fu-za-9b7o0/">https://leetcode.cn/problems/continuous-subarray-sum/solution/zi-ding-xiang-xia-fen-xi-shi-jian-fu-za-9b7o0/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/subarray-sum-equals-k/">560.和为k的子数组</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/subarray-sum-equals-k/solution/cong-shu-xue-de-jiao-du-tui-dao-yi-chong-w9y8/">https://leetcode.cn/problems/subarray-sum-equals-k/solution/cong-shu-xue-de-jiao-du-tui-dao-yi-chong-w9y8/</a></td>
</tr>
</tbody>
</table>
<h2 id="差分"><a class="markdownIt-Anchor" href="#差分"></a> 差分</h2>
<h3 id="算法框架-4"><a class="markdownIt-Anchor" href="#算法框架-4"></a> 算法框架</h3>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 从nums构造差分数组diff</span></span><br><span class="line"><span class="keyword">int</span> []diff = <span class="keyword">new</span> <span class="keyword">int</span>[n];<span class="comment">// diff[i]=nums[i]-nums[i-1]</span></span><br><span class="line">diss[<span class="number">0</span>]=nums[<span class="number">0</span>];</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;n;i++)</span><br><span class="line">    diff[i]=nums[i]-nums[i-<span class="number">1</span>];</span><br><span class="line"><span class="comment">// 从diff反推nums</span></span><br><span class="line">nums[<span class="number">0</span>]=diff[<span class="number">0</span>];</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;n;i++)</span><br><span class="line">    nums[i]=nums[i-<span class="number">1</span>]+diff[i];</span><br></pre></td></tr></table></figure>
<h3 id="题目汇总-5"><a class="markdownIt-Anchor" href="#题目汇总-5"></a> 题目汇总</h3>
<table>
<thead>
<tr>
<th>题目</th>
<th>题解</th>
</tr>
</thead>
<tbody>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/car-pooling/">1094.拼车</a></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/corporate-flight-bookings/">1109.航班预定统计</a></td>
<td></td>
</tr>
</tbody>
</table>
<h1 id="链表相关算法"><a class="markdownIt-Anchor" href="#链表相关算法"></a> 链表相关算法</h1>
<blockquote>
<p>先掌握双指针的方法，第二遍在学习栈或者递归的方法</p>
</blockquote>
<h2 id="反转链表"><a class="markdownIt-Anchor" href="#反转链表"></a> 反转链表</h2>
<h3 id="题目汇总-6"><a class="markdownIt-Anchor" href="#题目汇总-6"></a> 题目汇总</h3>
<table>
<thead>
<tr>
<th>题目</th>
<th>题解</th>
</tr>
</thead>
<tbody>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/reverse-linked-list/">206.反转链表</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/reverse-linked-list/solutions/2258879/san-chong-fang-fa-jie-jue-fan-zhuan-lian-0sjo/">https://leetcode.cn/problems/reverse-linked-list/solutions/2258879/san-chong-fang-fa-jie-jue-fan-zhuan-lian-0sjo/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/reverse-linked-list-ii/">92.反转链表 II</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/reverse-linked-list-ii/solution/shi-yong-206ti-si-lu-shi-jian-fu-za-du-o-tksn/">https://leetcode.cn/problems/reverse-linked-list-ii/solution/shi-yong-206ti-si-lu-shi-jian-fu-za-du-o-tksn/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/reverse-nodes-in-k-group/description/">25.K 个一组翻转链表</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/reverse-nodes-in-k-group/solutions/2258948/lei-bi-209fan-zhuan-lian-biao-si-lu-shi-vp1sp/">https://leetcode.cn/problems/reverse-nodes-in-k-group/solutions/2258948/lei-bi-209fan-zhuan-lian-biao-si-lu-shi-vp1sp/</a></td>
</tr>
</tbody>
</table>
<h3 id="206反转链表"><a class="markdownIt-Anchor" href="#206反转链表"></a> 206.反转链表</h3>
<h4 id="题目描述-5"><a class="markdownIt-Anchor" href="#题目描述-5"></a> 题目描述</h4>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506170529702.png" alt="image-20230506161611753"></p>
<h4 id="题解-5"><a class="markdownIt-Anchor" href="#题解-5"></a> 题解</h4>
<h5 id="迭代法"><a class="markdownIt-Anchor" href="#迭代法"></a> 迭代法</h5>
<p>从前向后反转，使cur指向当前节点，pre指向父节点，然后令<code>cur.next=pre</code>，由于反转之后cur.next修改为pre，所以需要临时保存cur.next。迭代结束后，cur指向null，pre指向最后一个节点，返回pre。头节点经过反转后变成尾节点，下一节点指向null，所以pre初始化为null。</p>
<p><strong>代码如下</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for singly-linked list.</span></span><br><span class="line"><span class="comment"> * public class ListNode &#123;</span></span><br><span class="line"><span class="comment"> * int val;</span></span><br><span class="line"><span class="comment"> * ListNode next;</span></span><br><span class="line"><span class="comment"> * ListNode() &#123;&#125;</span></span><br><span class="line"><span class="comment"> * ListNode(int val) &#123; this.val = val; &#125;</span></span><br><span class="line"><span class="comment"> * ListNode(int val, ListNode next) &#123; this.val = val; this.next = next; &#125;</span></span><br><span class="line"><span class="comment"> * &#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ListNode <span class="title">reverseList</span><span class="params">(ListNode head)</span> </span>&#123;</span><br><span class="line">        ListNode cur = head,pre = cull;</span><br><span class="line">        <span class="keyword">while</span>(cur!=<span class="keyword">null</span>)&#123;</span><br><span class="line">            ListNode t = cur.next;</span><br><span class="line">            cur.next = pre;</span><br><span class="line">            pre = cur;</span><br><span class="line">            cur = t;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> pre;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="递归法"><a class="markdownIt-Anchor" href="#递归法"></a> 递归法</h5>
<p><strong>从前向后反转</strong></p>
<p>递归函数<code>ListNode reverse(ListNode pre, ListNode cur)</code>定义为：反转pre和cur指向的节点,当cur==null时，返回pre，否则返回cur和cur.next反转后的结果。</p>
<p><strong>代码如下</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 递归:从前向后反转链表</span></span><br><span class="line">    <span class="function">ListNode <span class="title">reverse</span><span class="params">(ListNode pre, ListNode cur)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (cur == <span class="keyword">null</span>)</span><br><span class="line">            <span class="keyword">return</span> pre;</span><br><span class="line">        ListNode tmp = cur.next;</span><br><span class="line">        cur.next = pre;</span><br><span class="line">        <span class="keyword">return</span> reverse(cur, tmp);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ListNode <span class="title">reverseList</span><span class="params">(ListNode head)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> reverse(<span class="keyword">null</span>, head);<span class="comment">// 相当于迭代法中对pre和cur初始化</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="使用虚拟头节点从头插入反转链表"><a class="markdownIt-Anchor" href="#使用虚拟头节点从头插入反转链表"></a> 使用虚拟头节点从头插入反转链表</h5>
<p>建立一个dummy节点，使用cur迭代链表，对于每个cur节点从dummy插入，则得到的新的链表即为反转后的链表，由于每次插入的时候需要修改cur.next指向，所以需要临时存储cur.next，最后返回dummy.next。</p>
<p><strong>代码如下</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 使用虚拟节点头插法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ListNode <span class="title">reverseList</span><span class="params">(ListNode head)</span> </span>&#123;</span><br><span class="line">        ListNode dummy = <span class="keyword">new</span> ListNode(<span class="number">0</span>, <span class="keyword">null</span>);</span><br><span class="line">        ListNode cur = head;</span><br><span class="line">        ListNode tmp;</span><br><span class="line">        <span class="keyword">while</span> (cur != <span class="keyword">null</span>) &#123;</span><br><span class="line">            tmp = cur.next;<span class="comment">// 临时保存cur.next</span></span><br><span class="line">            <span class="comment">// 插入cur指向的节点</span></span><br><span class="line">            cur.next = dummy.next;</span><br><span class="line">            dummy.next = cur;</span><br><span class="line">            cur = tmp;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> dummy.next;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="92反转链表ii"><a class="markdownIt-Anchor" href="#92反转链表ii"></a> 92.反转链表II</h3>
<h4 id="题目描述-6"><a class="markdownIt-Anchor" href="#题目描述-6"></a> 题目描述</h4>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506163627287.png" alt="image-20230506163627287"></p>
<h4 id="题解-6"><a class="markdownIt-Anchor" href="#题解-6"></a> 题解</h4>
<h5 id="我的思路-5"><a class="markdownIt-Anchor" href="#我的思路-5"></a> 我的思路</h5>
<p>这里与<a target="_blank" rel="noopener" href="https://leetcode.cn/problems/reverse-linked-list/">206.反转链表</a>思路类似，只不过在反转前需要找到开始反转链表的位置，这里用p来保存待反转链表的父节点，但是当left==1时是没有父节点的，所以引入dummy节点保证当left取不同的值时代码的统一性。反转结束后，cur指向right后一个节点，pre指向right节点。然后修改p.next指向pre，p.next.next指向cur。</p>
<h5 id="我的代码-5"><a class="markdownIt-Anchor" href="#我的代码-5"></a> 我的代码</h5>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ListNode <span class="title">reverseBetween</span><span class="params">(ListNode head, <span class="keyword">int</span> left, <span class="keyword">int</span> right)</span> </span>&#123;</span><br><span class="line">        ListNode dummy = <span class="keyword">new</span> ListNode(<span class="number">0</span>, head);</span><br><span class="line">        ListNode p = dummy;<span class="comment">// 指向left的父节点</span></span><br><span class="line">        <span class="comment">// 寻找p的位置,因为p为left的父节点所以找到left-1的位置</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= left - <span class="number">1</span>; i++)</span><br><span class="line">            p = p.next;</span><br><span class="line">        <span class="comment">// 反转[left,right]链表</span></span><br><span class="line">        ListNode cur = p0.next, pre = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= right - left + <span class="number">1</span>; i++) &#123;</span><br><span class="line">            ListNode tmp = cur.next;</span><br><span class="line">            cur.next = pre;</span><br><span class="line">            pre = cur;</span><br><span class="line">            cur = tmp;</span><br><span class="line">        &#125;</span><br><span class="line">        p.next.next = cur;</span><br><span class="line">        p.next = pre;</span><br><span class="line">        <span class="keyword">return</span> dummy.next;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="25k-个一组翻转链表"><a class="markdownIt-Anchor" href="#25k-个一组翻转链表"></a> 25.K 个一组翻转链表</h3>
<h4 id="题目描述-7"><a class="markdownIt-Anchor" href="#题目描述-7"></a> 题目描述</h4>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230505223140112.png" alt="image-20230505223140112"></p>
<h4 id="题解-7"><a class="markdownIt-Anchor" href="#题解-7"></a> 题解</h4>
<h5 id="我的思路-6"><a class="markdownIt-Anchor" href="#我的思路-6"></a> 我的思路</h5>
<p>这里每次需要反转k个节点，当节点不足k个时不反转，所以需要统计一下链表的节点数目length，每反转一组节点后，带反转数目length减少k，当节点数不足k个时，迭代结束。反转k个节点的思路与<a target="_blank" rel="noopener" href="https://leetcode.cn/problems/reverse-linked-list-ii/">92.反转链表 II</a>过程完全相同，只是每反转一组后需要修改p指针的指向下一组待反转节点的父节点，而下一组节点的父节点为p.next，所以需要临时保存p.next，在反转结束后把p.next赋给p。</p>
<h5 id="我的代码-6"><a class="markdownIt-Anchor" href="#我的代码-6"></a> 我的代码</h5>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ListNode <span class="title">reverseKGroup</span><span class="params">(ListNode head, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> length = <span class="number">0</span>;</span><br><span class="line">        ListNode dummy = <span class="keyword">new</span> ListNode(<span class="number">0</span>, head);</span><br><span class="line">        ListNode p = dummy;</span><br><span class="line">        <span class="comment">// 计算链表数目length</span></span><br><span class="line">        <span class="keyword">while</span> (p.next != <span class="keyword">null</span>) &#123;</span><br><span class="line">            length++;</span><br><span class="line">            p = p.next;</span><br><span class="line">        &#125;</span><br><span class="line">        p = dummy;<span class="comment">// 指向每组第一个节点的父节点</span></span><br><span class="line">        <span class="keyword">while</span> (length &gt;= k) &#123;</span><br><span class="line">            length -= k;</span><br><span class="line">            ListNode cur = p.next, pre = <span class="keyword">null</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; k; i++) &#123;</span><br><span class="line">                ListNode tmp = cur.next;</span><br><span class="line">                cur.next = pre;</span><br><span class="line">                pre = cur;</span><br><span class="line">                cur = tmp;</span><br><span class="line">            &#125;</span><br><span class="line">            ListNode last = p.next;<span class="comment">// 为下一组带反转节点的父节点</span></span><br><span class="line">            p.next.next = cur;<span class="comment">// p的下一节点指向cur指向的节点</span></span><br><span class="line">            p.next = pre;<span class="comment">// p的下一节点为pre指向的节点</span></span><br><span class="line">            p = last;<span class="comment">// last为当前组最后一个节点</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> dummy.next;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="链表相交"><a class="markdownIt-Anchor" href="#链表相交"></a> 链表相交</h2>
<h3 id="题目汇总-7"><a class="markdownIt-Anchor" href="#题目汇总-7"></a> 题目汇总</h3>
<table>
<thead>
<tr>
<th>题目</th>
<th>题解</th>
</tr>
</thead>
<tbody>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/">160.相交链表</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/solutions/2260296/mo-ni-shi-jian-fu-za-du-onm-by-dpbirder-twvx/">我的题解</a>和<a target="_blank" rel="noopener" href="https://programmercarl.com/%E9%9D%A2%E8%AF%95%E9%A2%9802.07.%E9%93%BE%E8%A1%A8%E7%9B%B8%E4%BA%A4.html">代码随想录</a></td>
</tr>
</tbody>
</table>
<h3 id="160相交链表"><a class="markdownIt-Anchor" href="#160相交链表"></a> 160.相交链表</h3>
<h4 id="题目描述-8"><a class="markdownIt-Anchor" href="#题目描述-8"></a> 题目描述</h4>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506215809865.png" alt="image-20230506215809865"></p>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506215940117.png" alt="image-20230506215839409"></p>
<h4 id="题解-8"><a class="markdownIt-Anchor" href="#题解-8"></a> 题解</h4>
<h5 id="我的思路-7"><a class="markdownIt-Anchor" href="#我的思路-7"></a> 我的思路</h5>
<p>若两链表长度相同，则用两指针p1和p2分别遍历两链表，若存在<code>p1==p2</code>，则此时为两链表交点位置，反之不存在交点。</p>
<p>对于这题来说，链表的长度不是固定的，所以可以先计算出两链表长度差值diff，然后将长的链表向前移动diff个位置，此时就转化成了两个长度相同链表找交点的问题了。</p>
<h5 id="我的代码-7"><a class="markdownIt-Anchor" href="#我的代码-7"></a> 我的代码</h5>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ListNode <span class="title">getIntersectionNode</span><span class="params">(ListNode headA, ListNode headB)</span> </span>&#123;</span><br><span class="line">        ListNode pa = headA, pb = headB;</span><br><span class="line">        <span class="keyword">int</span> lengthA = <span class="number">0</span>, lengthB = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (pa != <span class="keyword">null</span>) &#123;<span class="comment">// 计算链表A的长度</span></span><br><span class="line">            lengthA++;</span><br><span class="line">            pa = pa.next;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span> (pb != <span class="keyword">null</span>) &#123;<span class="comment">// 计算链表B的长度</span></span><br><span class="line">            lengthB++;</span><br><span class="line">            pb = pb.next;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> diff = Math.abs(lengthA - lengthB);<span class="comment">// 计算两链表长度差值</span></span><br><span class="line">        pa = lengthA &gt; lengthB ? headA : headB;<span class="comment">// pa指向长链表</span></span><br><span class="line">        <span class="comment">// 长链表向前移动diff个位置</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; diff; i++)</span><br><span class="line">            pa = pa.next;</span><br><span class="line">        pb = lengthA &gt; lengthB ? headB : headA;<span class="comment">// pb指向短链表</span></span><br><span class="line">        <span class="keyword">while</span> (pa != <span class="keyword">null</span> &amp;&amp; pb != <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (pb == pa)</span><br><span class="line">                <span class="keyword">return</span> pb;</span><br><span class="line">            pa = pa.next;</span><br><span class="line">            pb = pb.next;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="快慢指针"><a class="markdownIt-Anchor" href="#快慢指针"></a> 快慢指针</h2>
<h3 id="题目汇总-8"><a class="markdownIt-Anchor" href="#题目汇总-8"></a> 题目汇总</h3>
<table>
<thead>
<tr>
<th>题目</th>
<th>题解</th>
</tr>
</thead>
<tbody>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/middle-of-the-linked-list/">876.链表的中间节点</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/middle-of-the-linked-list/solutions/2260298/kuai-man-zhi-zhen-shi-jian-fu-za-du-on-b-e7n0/">https://leetcode.cn/problems/middle-of-the-linked-list/solutions/2260298/kuai-man-zhi-zhen-shi-jian-fu-za-du-on-b-e7n0/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/linked-list-cycle/">141.环形链表</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/linked-list-cycle/solutions/2260687/kuai-man-zhi-zhen-shi-jian-fu-za-du-on-b-fb67/">https://leetcode.cn/problems/linked-list-cycle/solutions/2260687/kuai-man-zhi-zhen-shi-jian-fu-za-du-on-b-fb67/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/linked-list-cycle-ii/">142.环形链表II</a></td>
<td><a target="_blank" rel="noopener" href="https://programmercarl.com/0142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II.html#_142-%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8ii">代码随想录</a>和<a target="_blank" rel="noopener" href="https://leetcode.cn/problems/linked-list-cycle-ii/solutions/2260692/kuai-man-zhi-zhen-shi-jian-fu-za-du-on-b-zeed/">我的题解</a></td>
</tr>
<tr>
<td>143.重排链表</td>
<td></td>
</tr>
</tbody>
</table>
<h3 id="876链表的中间节点"><a class="markdownIt-Anchor" href="#876链表的中间节点"></a> 876.链表的中间节点</h3>
<h4 id="题目描述-9"><a class="markdownIt-Anchor" href="#题目描述-9"></a> 题目描述</h4>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230507200008224.png" alt="image-20230506215940117"></p>
<h4 id="题解-9"><a class="markdownIt-Anchor" href="#题解-9"></a> 题解</h4>
<h5 id="我的思路-8"><a class="markdownIt-Anchor" href="#我的思路-8"></a> 我的思路</h5>
<p>对于数组而言，由于数组是可以向前遍历的，所以使用左右指针同时向中间移动，当两指针相遇时即为答案。</p>
<p>对于链表而言，由于链表不能前向遍历，只能后向遍历，所以这题可以考虑使用快慢指针fast和slow，fast的移动速度是slow的二倍。当链表长度为奇数时，fast指向最后一个元素，slow指向中间节点，当链表长度为偶数时，fast指向null，slow指向中间右边节点。</p>
<h5 id="我的代码-8"><a class="markdownIt-Anchor" href="#我的代码-8"></a> 我的代码</h5>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ListNode <span class="title">middleNode</span><span class="params">(ListNode head)</span> </span>&#123;</span><br><span class="line">        ListNode slow = head, fast = head;</span><br><span class="line">        <span class="keyword">while</span> (fast != <span class="keyword">null</span> &amp;&amp; fast.next != <span class="keyword">null</span>) &#123;</span><br><span class="line">            fast = fast.next.next;</span><br><span class="line">            slow = slow.next;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> slow;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="141环形链表"><a class="markdownIt-Anchor" href="#141环形链表"></a> 141.环形链表</h3>
<h4 id="题目描述-10"><a class="markdownIt-Anchor" href="#题目描述-10"></a> 题目描述</h4>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506215839409.png" alt="image-20230506220018533"></p>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506220018533.png" alt="image-20230506220136263"></p>
<h4 id="题解-10"><a class="markdownIt-Anchor" href="#题解-10"></a> 题解</h4>
<h5 id="我的思路-9"><a class="markdownIt-Anchor" href="#我的思路-9"></a> 我的思路</h5>
<p>这题要判断链表中是否有环，类似于<a target="_blank" rel="noopener" href="https://leetcode.cn/problems/middle-of-the-linked-list/">876.链表的中间节点</a>使用快慢指针fast和slow，考虑相对速度，fast的速度是slow速度的两倍，也就是fast会比slow每次多走一步，如果存在环的话，则fast与slow在环内一定会相遇，因为fast每次会追赶slow一步。最终若fast与slow相遇则一定存在环，反之没有环。由于fast</p>
<h5 id="我的代码-9"><a class="markdownIt-Anchor" href="#我的代码-9"></a> 我的代码</h5>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">hasCycle</span><span class="params">(ListNode head)</span> </span>&#123;</span><br><span class="line">        ListNode fast = head, slow = head;</span><br><span class="line">        <span class="keyword">while</span> (fast != <span class="keyword">null</span> &amp;&amp; fast.next!=<span class="keyword">null</span>) &#123;</span><br><span class="line">            fast = fast.next.next;</span><br><span class="line">            slow = slow.next;</span><br><span class="line">            <span class="keyword">if</span>(fast==slow) <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="142环形链表ii"><a class="markdownIt-Anchor" href="#142环形链表ii"></a> 142.环形链表II</h3>
<h4 id="题目描述-11"><a class="markdownIt-Anchor" href="#题目描述-11"></a> 题目描述</h4>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230507195948359.png" alt="image-20230507195948359"></p>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230506220136263.png" alt="image-20230507200008224"></p>
<h4 id="题解-11"><a class="markdownIt-Anchor" href="#题解-11"></a> 题解</h4>
<h5 id="我的思路-10"><a class="markdownIt-Anchor" href="#我的思路-10"></a> 我的思路</h5>
<p>这题要计算的是环的入口位置，由<a target="_blank" rel="noopener" href="https://leetcode.cn/problems/linked-list-cycle/">141.环形链表</a>最后结果可以知道，若链表存在环，则fast与slow相遇的位置一定在环内。设环的入口距离头节点位置为x，两指针相遇位置具环入口位置分别为y和z，则题目要求的即为x，如下图。因为fast的速度是slow的二倍，所以快指针走的距离是慢指针的二倍，即<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>2</mn><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mi>y</mi><mo stretchy="false">)</mo><mo>=</mo><mi>x</mi><mo>+</mo><mi>y</mi><mo>+</mo><mi>n</mi><mo stretchy="false">(</mo><mi>y</mi><mo>+</mo><mi>z</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">2(x+y)=x+y+n(y+z)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">2</span><span class="mopen">(</span><span class="mord mathdefault">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathdefault">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.7777700000000001em;vertical-align:-0.19444em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault">n</span><span class="mopen">(</span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.04398em;">z</span><span class="mclose">)</span></span></span></span>，把x放在等式左边，则<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>x</mi><mo>=</mo><mo stretchy="false">(</mo><mi>n</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo><mo stretchy="false">(</mo><mi>y</mi><mo>+</mo><mi>z</mi><mo stretchy="false">)</mo><mo>+</mo><mi>z</mi></mrow><annotation encoding="application/x-tex">x=(n-1)(y+z)+z</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathdefault">n</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">1</span><span class="mclose">)</span><span class="mopen">(</span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.04398em;">z</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.04398em;">z</span></span></span></span>，这个等式说明，若此时有两个指针p1和p2分别从头节点和两指针相遇节点以相同的速度出发，则最终他们会在环入口处相遇，最终他们相遇的位置就是环入口位置。</p>
<p><img src="https://code-thinking-1253855093.file.myqcloud.com/pics/20220925103433.png" alt="img"></p>
<h5 id="我的代码-10"><a class="markdownIt-Anchor" href="#我的代码-10"></a> 我的代码</h5>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ListNode <span class="title">detectCycle</span><span class="params">(ListNode head)</span> </span>&#123;</span><br><span class="line">        ListNode fast = head, slow = head;</span><br><span class="line">        <span class="keyword">while</span> (fast != <span class="keyword">null</span> &amp;&amp; fast.next != <span class="keyword">null</span>) &#123;</span><br><span class="line">            fast = fast.next.next;</span><br><span class="line">            slow = slow.next;</span><br><span class="line">            <span class="keyword">if</span> (fast == slow) &#123;</span><br><span class="line">                <span class="comment">// 两指针相遇</span></span><br><span class="line">                slow = head;</span><br><span class="line">                <span class="comment">// 下一次相遇一定在入口位置</span></span><br><span class="line">                <span class="keyword">while</span> (fast != slow) &#123;</span><br><span class="line">                    fast = fast.next;</span><br><span class="line">                    slow = slow.next;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> fast;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="143重排链表"><a class="markdownIt-Anchor" href="#143重排链表"></a> 143.重排链表</h3>
<h2 id="约翰环问题"><a class="markdownIt-Anchor" href="#约翰环问题"></a> 约翰环问题</h2>
<h1 id="字符串相关算法"><a class="markdownIt-Anchor" href="#字符串相关算法"></a> 字符串相关算法</h1>
<h1 id="其他算法"><a class="markdownIt-Anchor" href="#其他算法"></a> 其他算法</h1>
<h2 id="位运算"><a class="markdownIt-Anchor" href="#位运算"></a> 位运算</h2>
<h3 id="模板"><a class="markdownIt-Anchor" href="#模板"></a> 模板</h3>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">求n的二进制数表示中第k位是几</span></span><br><span class="line"><span class="comment">1. 将第k位移动到最后一位</span></span><br><span class="line"><span class="comment">2. 看最后一位是几</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">n &gt;&gt; k &amp; <span class="number">1</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">lowbit(x): 返回x最后一位1是多少</span></span><br><span class="line"><span class="comment">eg: (x)B = 1010B, 则lowbit(x) = 10B = 2D; (x)B=101000, 则lowbit(x) = 1000B = 8D;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"> n &amp; -n</span><br></pre></td></tr></table></figure>
<h3 id="练习"><a class="markdownIt-Anchor" href="#练习"></a> 练习</h3>
<p><a target="_blank" rel="noopener" href="https://www.acwing.com/problem/content/803/">二进制中1的个数</a></p>
<h2 id="离散化"><a class="markdownIt-Anchor" href="#离散化"></a> 离散化</h2>
<h3 id="模板-2"><a class="markdownIt-Anchor" href="#模板-2"></a> 模板</h3>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; alls; <span class="comment">// 存储所有待离散化的值(存储待离散化数组的下标)</span></span><br><span class="line">sort(alls.<span class="built_in">begin</span>(), alls.<span class="built_in">end</span>()); <span class="comment">// 将所有值排序</span></span><br><span class="line">alls.erase(unique(alls.<span class="built_in">begin</span>(), alls.<span class="built_in">end</span>()), alls.<span class="built_in">end</span>());	<span class="comment">// 去掉重复元素</span></span><br><span class="line">	</span><br><span class="line"><span class="comment">// 二分求出x对应的离散化的值，查找第一个大于等于x的数的下标</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">find</span><span class="params">(<span class="keyword">int</span> x)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> l = <span class="number">0</span>, r = alls.<span class="built_in">size</span>() - <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span> (l &lt; r)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">int</span> mid = l + r &gt;&gt; <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">if</span> (alls[mid] &gt;= x) r = mid;</span><br><span class="line">        <span class="keyword">else</span> l = mid + <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> r + <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 手动实现unique</span></span><br><span class="line"><span class="comment">// 作用：将数组alls中不重复元素放在alls的前面，重复的元素放在后面，返回不重复元素的最后一个元素后一位的迭代器</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">去重数组应当满足的两种性质：</span></span><br><span class="line"><span class="comment">1. 第一个元素</span></span><br><span class="line"><span class="comment">2. a[i]!=a[i-1]</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;::<span class="function">iterator <span class="title">unique</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp;as)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> j = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; a.<span class="built_in">size</span>(); i ++)</span><br><span class="line">        <span class="keyword">if</span>(!i || a[i] != a[i - <span class="number">1</span>])</span><br><span class="line">            a[j ++ ] = a[i];</span><br><span class="line">    <span class="keyword">return</span> a.<span class="built_in">begin</span>() + j;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="练习-2"><a class="markdownIt-Anchor" href="#练习-2"></a> 练习</h3>
<p><a target="_blank" rel="noopener" href="https://www.acwing.com/problem/content/804/">区间和</a></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">300010</span>;</span><br><span class="line"><span class="comment">//一般只有两个元素的结构体使用pair表示</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="built_in">pair</span>&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; PII;</span><br><span class="line"><span class="keyword">int</span> a[N],s[N];<span class="comment">//a[]存储离散化后的结果，s[]存储前缀和</span></span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; alls;<span class="comment">//存储待离散化数组下标</span></span><br><span class="line"><span class="built_in">vector</span>&lt;PII&gt; add,query;</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cin</span>&gt;&gt;n&gt;&gt;m;</span><br><span class="line">    <span class="keyword">int</span> x,c;</span><br><span class="line">    <span class="comment">//先将需要去重的数组下标，读入alls中</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;n;i++) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cin</span>&gt;&gt;x&gt;&gt;c;</span><br><span class="line">        add.push_back(&#123;x,c&#125;);</span><br><span class="line">        alls.push_back(x);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;m;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">int</span> l,r;</span><br><span class="line">        <span class="built_in">cin</span>&gt;&gt;l&gt;&gt;r;</span><br><span class="line">        query.push_back(&#123;l,r&#125;);</span><br><span class="line">        alls.push_back(l);</span><br><span class="line">        alls.push_back(r);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    sort(alls.<span class="built_in">begin</span>(),alls.<span class="built_in">end</span>());<span class="comment">//排序</span></span><br><span class="line">    alls.erase(unique(alls.<span class="built_in">begin</span>(),alls.<span class="built_in">end</span>()),alls.<span class="built_in">end</span>());<span class="comment">//去重</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">//处理插入</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">auto</span> &amp;item : add)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">int</span> x=<span class="built_in">find</span>(item.first);</span><br><span class="line">        a[x]+=item.second;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//预处理前缀和</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=alls.<span class="built_in">size</span>();i++) s[i]=s[i<span class="number">-1</span>]+a[i];</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//处理询问</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">auto</span> &amp;item : query)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">int</span> l=<span class="built_in">find</span>(item.first),r=<span class="built_in">find</span>(item.second);</span><br><span class="line">        <span class="built_in">cout</span>&lt;&lt;s[r]-s[l<span class="number">-1</span>]&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="区间问题"><a class="markdownIt-Anchor" href="#区间问题"></a> 区间问题</h2>
<h3 id="模板-3"><a class="markdownIt-Anchor" href="#模板-3"></a> 模板</h3>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">思路：</span></span><br><span class="line"><span class="comment">1. 将所有区间按照左端点排序</span></span><br><span class="line"><span class="comment">2. 遍历待合并区间</span></span><br><span class="line"><span class="comment">	a. 初始化st,ed表示维护的区间</span></span><br><span class="line"><span class="comment">    b. 如果当前维护的区间与该区间没有交集，则将当前维护的区间存入结果，并更新维护的区间</span></span><br><span class="line"><span class="comment">    c. 若维护的区间与当前区间有交集，则更新ed成为最右边的端点</span></span><br><span class="line"><span class="comment">3. 遍历结束，将维护区间存入结果(应为该区间在循环中并没有被存入结果中)</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 将所有存在交集的区间合并</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">merge</span><span class="params">(<span class="built_in">vector</span>&lt;PII&gt; &amp;segs)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">vector</span>&lt;PII&gt; res;</span><br><span class="line">    </span><br><span class="line">    sort(segs.<span class="built_in">begin</span>(), segs.<span class="built_in">end</span>());</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> st = <span class="number">-2e9</span>, ed = <span class="number">-2e9</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">auto</span> seg : segs)</span><br><span class="line">        <span class="keyword">if</span> (ed &lt; seg.first)</span><br><span class="line">        &#123;         </span><br><span class="line">          	<span class="keyword">if</span> (st != <span class="number">-2e9</span>) res.push_back(&#123;st, ed&#125;);</span><br><span class="line">            st = seg.first, ed = seg.second;</span><br><span class="line">        &#125;</span><br><span class="line">    	<span class="keyword">else</span> ed = <span class="built_in">max</span>(ed, seg.second);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (st != <span class="number">-2e9</span>) res.push_back(&#123;st, ed&#125;);</span><br><span class="line"></span><br><span class="line">    segs = res;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="练习-3"><a class="markdownIt-Anchor" href="#练习-3"></a> 练习</h3>
<table>
<thead>
<tr>
<th>题目</th>
<th>题解</th>
</tr>
</thead>
<tbody>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/merge-intervals/">56.合并区间</a></td>
<td></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/insert-interval/">57.插入区间</a></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>

    </div>

    
    
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E7%AE%97%E6%B3%95/" rel="tag"># 算法</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/03/28/%E5%AD%97%E7%AC%A6%E4%B8%B2/" rel="prev" title="字符串">
      <i class="fa fa-chevron-left"></i> 字符串
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/04/10/%E5%A0%86/" rel="next" title="堆">
      堆 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E7%9B%B8%E5%85%B3%E7%AE%97%E6%B3%95"><span class="nav-text"> 数组相关算法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE"><span class="nav-text"> 二分查找</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E6%A1%86%E6%9E%B6"><span class="nav-text"> 算法框架</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%B1%87%E6%80%BB"><span class="nav-text"> 题目汇总</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#704%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE"><span class="nav-text"> 704.二分查找</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#34%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE"><span class="nav-text"> 34.在排序数组中查找元素的第一个和最后一个位置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-2"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-2"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-2"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-2"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%8C%E6%8C%87%E9%92%88"><span class="nav-text"> 双指针</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%B1%87%E6%80%BB-2"><span class="nav-text"> 题目汇总</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3"><span class="nav-text"> 滑动窗口</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E6%A1%86%E6%9E%B6-2"><span class="nav-text"> 算法框架</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%B1%87%E6%80%BB-3"><span class="nav-text"> 题目汇总</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#209%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E5%AD%90%E6%95%B0%E7%BB%84"><span class="nav-text"> 209.长度最小子数组</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-3"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-3"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-3"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-3"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#904%E6%B0%B4%E6%9E%9C%E6%88%90%E7%AF%AE"><span class="nav-text"> 904.水果成篮</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-4"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-4"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-4"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-4"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E7%BC%80%E5%92%8C"><span class="nav-text"> 前缀和</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E6%A1%86%E6%9E%B6-3"><span class="nav-text"> 算法框架</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%B1%87%E6%80%BB-4"><span class="nav-text"> 题目汇总</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B7%AE%E5%88%86"><span class="nav-text"> 差分</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E6%A1%86%E6%9E%B6-4"><span class="nav-text"> 算法框架</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%B1%87%E6%80%BB-5"><span class="nav-text"> 题目汇总</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%93%BE%E8%A1%A8%E7%9B%B8%E5%85%B3%E7%AE%97%E6%B3%95"><span class="nav-text"> 链表相关算法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8"><span class="nav-text"> 反转链表</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%B1%87%E6%80%BB-6"><span class="nav-text"> 题目汇总</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#206%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8"><span class="nav-text"> 206.反转链表</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-5"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-5"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E8%BF%AD%E4%BB%A3%E6%B3%95"><span class="nav-text"> 迭代法</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E9%80%92%E5%BD%92%E6%B3%95"><span class="nav-text"> 递归法</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E8%99%9A%E6%8B%9F%E5%A4%B4%E8%8A%82%E7%82%B9%E4%BB%8E%E5%A4%B4%E6%8F%92%E5%85%A5%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8"><span class="nav-text"> 使用虚拟头节点从头插入反转链表</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#92%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8ii"><span class="nav-text"> 92.反转链表II</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-6"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-6"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-5"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-5"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#25k-%E4%B8%AA%E4%B8%80%E7%BB%84%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8"><span class="nav-text"> 25.K 个一组翻转链表</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-7"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-7"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-6"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-6"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%93%BE%E8%A1%A8%E7%9B%B8%E4%BA%A4"><span class="nav-text"> 链表相交</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%B1%87%E6%80%BB-7"><span class="nav-text"> 题目汇总</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#160%E7%9B%B8%E4%BA%A4%E9%93%BE%E8%A1%A8"><span class="nav-text"> 160.相交链表</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-8"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-8"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-7"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-7"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BF%AB%E6%85%A2%E6%8C%87%E9%92%88"><span class="nav-text"> 快慢指针</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%B1%87%E6%80%BB-8"><span class="nav-text"> 题目汇总</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#876%E9%93%BE%E8%A1%A8%E7%9A%84%E4%B8%AD%E9%97%B4%E8%8A%82%E7%82%B9"><span class="nav-text"> 876.链表的中间节点</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-9"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-9"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-8"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-8"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#141%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8"><span class="nav-text"> 141.环形链表</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-10"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-10"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-9"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-9"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#142%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8ii"><span class="nav-text"> 142.环形链表II</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-11"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-11"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-10"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-10"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#143%E9%87%8D%E6%8E%92%E9%93%BE%E8%A1%A8"><span class="nav-text"> 143.重排链表</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BA%A6%E7%BF%B0%E7%8E%AF%E9%97%AE%E9%A2%98"><span class="nav-text"> 约翰环问题</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9B%B8%E5%85%B3%E7%AE%97%E6%B3%95"><span class="nav-text"> 字符串相关算法</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%B6%E4%BB%96%E7%AE%97%E6%B3%95"><span class="nav-text"> 其他算法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%8D%E8%BF%90%E7%AE%97"><span class="nav-text"> 位运算</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A8%A1%E6%9D%BF"><span class="nav-text"> 模板</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%83%E4%B9%A0"><span class="nav-text"> 练习</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%A6%BB%E6%95%A3%E5%8C%96"><span class="nav-text"> 离散化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A8%A1%E6%9D%BF-2"><span class="nav-text"> 模板</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%83%E4%B9%A0-2"><span class="nav-text"> 练习</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8C%BA%E9%97%B4%E9%97%AE%E9%A2%98"><span class="nav-text"> 区间问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A8%A1%E6%9D%BF-3"><span class="nav-text"> 模板</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%83%E4%B9%A0-3"><span class="nav-text"> 练习</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="dpbirder"
      src="/images/boy.jpg">
  <p class="site-author-name" itemprop="name">dpbirder</p>
  <div class="site-description" itemprop="description">目前研一就读于西安电子科技大学</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">51</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="sidebar-button motion-element">
    <a onclick="Chatra('openChat', true);"><i class="fa fa-comment"></i>
    Chat
  </a>
  </div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/zhengzp2019" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhengzp2019" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://mail.qq.com/" title="E-Mail → https:&#x2F;&#x2F;mail.qq.com&#x2F;" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        
  <div class="languages">
    <label class="lang-select-label">
      <i class="fa fa-language"></i>
      <span>简体中文</span>
      <i class="fa fa-angle-up" aria-hidden="true"></i>
    </label>
    <select class="lang-select" data-canonical="">
      
        <option value="zh-CN" data-href="/2021/04/03/%E5%9F%BA%E7%A1%80%E7%AE%97%E6%B3%95/" selected="">
          简体中文
        </option>
      
        <option value="en" data-href="/en/2021/04/03/%E5%9F%BA%E7%A1%80%E7%AE%97%E6%B3%95/" selected="">
          English
        </option>
      
    </select>
  </div>

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">dpbirder</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">225k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:25</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>


  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>




  <script>
    (function(d, w, c) {
      w.ChatraID = 'Z8KH5JTPhxGC84tKb';
      var s = d.createElement('script');
      w[c] = w[c] || function() {
        (w[c].q = w[c].q || []).push(arguments);
      };
      s.async = true;
      s.src = 'https://call.chatra.io/chatra.js';
      if (d.head) d.head.appendChild(s);
    })(document, window, 'Chatra');
  </script>









  

  

  

</body>
</html>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>
